#include "apue.h"
#include <errno.h>
#include <sys/resource.h>
#define BUFSIZE 100 

void sig_xfsz(int signo) {
    fprintf(stderr, "signo: %d, siginfo: %s\n", signo, strsignal(signo));
}

int main(void) {
    int n;
    int m;
    char buf[BUFSIZE];
    struct rlimit limit; 

    limit.rlim_cur= 1024;
    limit.rlim_max= 1024;
    if (setrlimit(RLIMIT_FSIZE, &limit) < 0)
        err_sys("setrlimit(RLIMIT_FISIZE) error ");

    //getrlimit(RLIMIT_FSIZE, &limit);
    //printf("%ld %ld\n", limit.rlim_cur, limit.rlim_max);
    //exit(0);

    if (signal_intr(SIGXFSZ, sig_xfsz) == SIG_ERR)
        err_sys("signal(SIGXFSZ) error");

    while ((n= read(STDIN_FILENO, buf, BUFSIZE)) > 0)
        if ((m= write(STDOUT_FILENO, buf, n)) != n) {
            fprintf(stderr, "m= %d, n= %d, errno= %d\n", m, n, errno);
            if (m < 0)
                err_sys("write error");
        }

    if (n < 0)
        err_sys("read_error");
    exit(0);
}
